package com.aiju.cash.ui.main;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aiju.base.activity.BaseActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.login.LoginActivity;
import com.aiju.cash.widgit.DotView;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-12-20 14:42
 * @version: V1.0
 * @Description: 引导页
 */
public class GuideActivity extends BaseActivity {

    private ViewPager mViewPager;
    private List<View> views = new ArrayList<>();

    private DotView mDotView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initViews();
    }

    @Override
    protected void setStatusBar() {
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintColor(getTitleColor());
        tintManager.setStatusBarAlpha(0);
    }

    private void initViews() {
        List<Integer> images = new ArrayList<>();
        images.add(R.mipmap.guide1);
        images.add(R.mipmap.guide2);
        images.add(R.mipmap.guide3);
        for (int i = 0; i < images.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setImageResource(images.get(i));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            views.add(imageView);
        }
        mDotView = (DotView) findViewById(R.id.dotview);

        mViewPager = (ViewPager) findViewById(R.id.vp_guide);
        mViewPager.setAdapter(new ViewPagerAdapter(views));
        mDotView.setNum(views.size());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == views.size() - 1) {
                    findViewById(R.id.tv_guide).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.tv_guide).setVisibility(View.GONE);
                }
                mDotView.setSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        findViewById(R.id.tv_guide).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences preferences = getSharedPreferences("first_pref", MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("isFirstIn", false);
                editor.commit();
                startActivity(LoginActivity.class);
            }
        });
    }

    private final class ViewPagerAdapter extends PagerAdapter {
        private List<View> list;

        public ViewPagerAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {

            if (list != null && list.size() > 0) {
                return list.size();
            } else {
                return 0;
            }
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

    }
}